<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
         <el-form-item label="任务名称">
            <el-input v-model="queryParams.governCheckTaskName" placeholder="请输入任务名称" clearable style="width: 240px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="任务类型">
            <el-input v-model="queryParams.governTaskTypeName" placeholder="请输入任务类型" clearable style="width: 240px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="检查类型">
            <el-select v-model="queryParams.governCheckTaskTypeName" placeholder="请选择检查类型" clearable>
               <el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
            </el-select>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" @click="handleAddTask"
               v-hasPermi="['check:governCheckTaskInfo:add']">新增</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" :disabled="multiple" @click="handleDelete"
               v-hasPermi="['check:governCheckTaskInfo:remove']">删除</el-button>
         </el-col>
         <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-card class="card-box">
         <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="40" align="center" fixed="left" />
            <el-table-column label="序号" width="55" type="index" align="center" prop="governTaskInfoId" fixed="left" />
            <el-table-column label="任务名称" align="center" prop="governCheckTaskName" width="320"
               :show-overflow-tooltip="true" />
            <el-table-column label="任务类型" align="center" prop="governTaskTypeName" width="320"
               :show-overflow-tooltip="true" />
            <el-table-column label="检查类型" align="center" prop="governCheckTaskTypeName" width="100"></el-table-column>
            <el-table-column label="任务开始时间" align="center" prop="governTaskStartTime" width="160">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.governTaskStartTime) }}</span>
               </template>
            </el-table-column>
            <el-table-column label="任务结束时间" align="center" prop="governTaskEndTime" width="160">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.governTaskEndTime) }}</span>
               </template>
            </el-table-column>
            <el-table-column label="任务完成时间" align="center" prop="governTaskCompleteTime" width="160">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.governTaskCompleteTime) }}</span>
               </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="400" class-name="small-padding fixed-width" fixed="right">
               <template #default="scope">
                  <el-button type="text" icon="document-copy" @click="handleTaskDecompose(scope.row)"
                     :disabled="scope.row.governCheckTaskTypeName !== '企业自查'"
                     v-hasPermi="['check:governCheckTaskInfo:add']">任务分解</el-button>
                  <el-button type="text" icon="document-add" @click="handleAddHidden(scope.row)"
                     v-hasPermi="['check:governCheckTaskInfo:add']">隐患录入</el-button>
                  <el-button type="text" icon="finished" @click="handleTask(scope.row)"
                     v-hasPermi="['check:governCheckTaskInfo:edit']">任务完成</el-button>
                  <el-button type="text" icon="view" @click="showTaskDetail(scope.row)"
                     v-hasPermi="['check:governCheckTaskInfo:query']">查看</el-button>
               </template>
            </el-table-column>
         </el-table></el-card>


      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize" @pagination="getList" />

      <!-- 新增任务配置对话框 -->
      <el-dialog :title="title" v-model="addOpen" width="55%" append-to-body>
         <el-form ref="addTask" :model="form" :rules="rules" label-width="150px" style="margin-right: 90px">
            <el-row>
               <el-col :span="12">
                  <el-form-item label="任务名称" prop="governCheckTaskId">
                     <el-select v-model="form.governCheckTaskId" placeholder="请输入任务名称" clearable style="width: 100%;">
                        <el-option v-for="dic in checkTaskList" :key="dic.governCheckTaskId"
                           :label="dic.governCheckTaskName" :value="dic.governCheckTaskId" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="检查类型" prop="governCheckTaskTypeName">
                     <el-select v-model="form.governCheckTaskTypeName" placeholder="请选择检查类型" clearable
                        style="width: 100%;">
                        <el-option v-for="value in addTaskTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>

      <!-- 隐患录入对话框 -->
      <el-dialog :title="title" v-model="hiddenOpen" width="55%" append-to-body>
         <el-form ref="addHidden" :model="formH" :rules="rules" label-width="150px" style="margin-right: 30px">
            <el-divider content-position="left">登记信息</el-divider>
            <el-row v-if="false">
               <el-col :span="12">
                  <el-form-item label="登记人">
                     <el-input v-model="formH.registerUserName" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="登记时间">
                     <el-date-picker v-model="formH.governCreateDate" type="date" placeholder="选择日期"
                        style="width: 100%" />
                  </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="隐患名称:" prop="hiddenInventoryName">
                     <el-input v-model="formH.hiddenInventoryName" placeholder="请输入隐患名称" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="隐患来源:" prop="hiddenSource">
                     <el-select v-model="formH.hiddenSource" placeholder="请选择隐患来源" clearable style="width: 100%;">
                        <el-option v-for="value in sourceList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="隐患分类:" prop="hiddenType">
                     <el-select v-model="formH.hiddenType" placeholder="请选择隐患分类" clearable style="width: 100%;">
                        <el-option v-for="value in hiddenTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="隐患等级:" prop="hiddenLevel">
                     <el-select v-model="formH.hiddenLevel" placeholder="请选择隐患等级" clearable style="width: 100%;">
                        <el-option v-for="value in hiddenLevelList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="所属装置:" label-position="right">
                     <el-select v-model="formH.deviceName" placeholder="请选择所属装置" clearable style="width: 100%;" @change="fChangeDevice">
                        <el-option v-for="device in deviceList" :key="device.id" :label="device.deviceName"
                           :value="device" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="所属部门:" label-position="right">
                     <TreeSelect v-model="formH.deptName" :options="deptNameList" placeholder="请选择所属部门"
                        @change="changeDept" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="排查级别:">
                     <el-select v-model="formH.checkLevel" placeholder="请选择排查级别" clearable style="width: 100%;"
                        :disabled="true">
                        <el-option v-for="value in checkList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="排查类型:" prop="hiddenCheckType">
                     <el-select v-model="formH.hiddenCheckType" placeholder="请选择排查类型" clearable style="width: 100%;"
                        :disabled="true">
                        <el-option v-for="value in taskTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="危害类型:" prop="harmType">
                     <el-select v-model="formH.harmType" placeholder="请选择危害类型" clearable style="width: 100%;">
                        <el-option v-for="value in WeiHaiTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="治理资金:" label-position="right">
                     <el-input v-model="formH.hiddenManagingFunds" placeholder="请输入治理资金" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="隐患治理类型:" prop="dangerManageType">
                     <el-select v-model="formH.dangerManageType" placeholder="请选择隐患治理类型" clearable style="width: 100%;">
                        <el-option v-for="value in ZhiLiTypeList" :key="value" :label="value" :value="value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="隐患治理期限:" prop="rectificationEndTime">
                     <el-date-picker v-model="formH.rectificationEndTime" type="date" placeholder="选择日期"
                        style="width: 100%;" />
                  </el-form-item>
               </el-col>
            </el-row>
            <el-form-item label="隐患描述:" prop="hiddenInfo">
               <el-input v-model="formH.hiddenInfo" type="textarea" :rows="3" placeholder="请输入隐患描述" />
            </el-form-item>
            <el-form-item label="形成原因:">
               <el-input v-model="formH.hiddenEntryBasis" type="textarea" :rows="3" placeholder="请输入形成原因" />
            </el-form-item>
            <el-form-item label="整改措施:">
               <el-input v-model="formH.hiddenRectificationMeasure" type="textarea" :rows="3" placeholder="请输入整改措施" />
            </el-form-item>
			
<!--        <el-form-item label="隐患图片:">
               <el-image style="width: 100px; height: 100px" :src="formH.hiddenFileUrl" />
            </el-form-item> -->
			
            <el-form-item label="检查项:">
               <el-select v-model="formH.governCheckTaskItemId" placeholder="请选择检查项" clearable style="width: 100%;"
                  @change="governCheckChanged">
                  <el-option v-for="item in governCheckNameList" style="width:1000px;" :key="item.id"
                     :label="item.governCheckName + item.governCheckContent" :value="item.governCheckTaskItemId" />
               </el-select>
            </el-form-item>
            <el-form-item label="检查依据:">
               <el-input v-model="formH.governCheckBasis" type="textarea" :rows="3" placeholder="请输入检查依据"
                  :disabled="true" />
            </el-form-item>
            <el-form-item label="检查方式:">
               <el-input v-model="formH.governCheckWay" type="textarea" :rows="3" placeholder="请输入检查方式"
                  :disabled="true" />
            </el-form-item>
            <el-form-item label="适用场合:">
               <el-input v-model="formH.governApplicablePlace" type="textarea" :rows="3" placeholder="请输入适用场合"
                  :disabled="true" />
            </el-form-item>

            <el-row>
               <el-col :span="12">
                  <el-form-item label="扣分:">
                     <el-select v-model="formH.governCheckTaskItemScoreId" placeholder="请选择扣分" style="width: 100%;"
                        @change="governCheckScoreChanged">
                        <el-option v-for="item in deductPointList" :key="item.id" :label="item.governDeductPoint"
                           :value="item.governCheckTaskItemScoreId" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="否决项:">
                     <el-select v-model="formH.governIsReject" placeholder="请选择否决项" clearable style="width: 100%;">
                        <el-option v-for="item in [{ id: 1, value: '是' }, { id: 0, value: '否' }]" :key="item.id"
                           :label="item.value" :value="item.id" />
                     </el-select>
                  </el-form-item>
               </el-col>
            </el-row>
            <el-form-item label="扣分说明:">
               <el-input v-model="formH.governDeductIllustrate" type="textarea" :rows="3" placeholder="请输入扣分说明"
                  :disabled="true" />
            </el-form-item>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="罚款金额:">
                     <el-input v-model="formH.hiddenCheckPay" placeholder="请输入罚款金额" />
                  </el-form-item>
               </el-col>
            </el-row>

            <el-form-item label="文件上传:" prop="fileId">
               <FileUpload :limit="1" :fileSize="30" v-model="files"></FileUpload>
            </el-form-item>

            <el-row>
               <el-col :span="12">
                  <el-form-item label="整改人:" label-position="right" prop="rectificationUserName">
                     <el-input v-model="formH.rectificationUserName" readonly placeholder="请选择整改人"
                        @click="onInnerOpen('rectificationUser')" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="复核人:" label-position="right" prop="reviewUserName">
                     <el-input v-model="formH.reviewUserName" readonly placeholder="请选择复核人"
                        @click="onInnerOpen('reviewUser')" />
                  </el-form-item>
               </el-col>
            </el-row>


            <!--

            <el-divider content-position="left">整改情况</el-divider>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="整改人:" label-position="right">
                     <el-input v-model="formH.rectificationUserName" readonly placeholder="请选择整改人"
                        @click="onInnerOpen('rectificationUser')" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="整改时间:">
                     <el-date-picker v-model="formH.hiddenCompleteTime" type="date" placeholder="选择日期"
                        style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col>
                  <el-form-item label="整改情况:">
                     <el-input v-model="formH.hiddenCompleteInfo" type="textarea" :rows="3" placeholder="请输入整改情况" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="整改图片:">
                     <el-image style="width: 100px; height: 100px" :src="formH.completeHiddenFileUrl" />
                  </el-form-item>
               </el-col>
            </el-row>
            <el-divider content-position="left">复核情况</el-divider>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="复核人:" label-position="right">
                     <el-input v-model="formH.reviewUserName" readonly placeholder="请选择复核人"
                        @click="onInnerOpen('reviewUser')" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="复核时间:" style="width: 100%">
                     <el-date-picker v-model="formH.hiddenReviewTime" type="date" placeholder="选择日期"
                        style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col>
                  <el-form-item label="复核情况:">
                     <el-input v-model="formH.hiddenReviewInfo" type="textarea" :rows="3" placeholder="请输入复核情况" />
                  </el-form-item>
               </el-col>
            </el-row>
-->
            <TableSelect ref="userTableSelect" :queryApi="listUser" :options="[]" :rowKey="'userId'" rowLabel="nickName"
               @onConfirm="onUserConfirm" rowLabelText="用户昵称" :maxNum="1">
               <template #formSlot="{ handleQuery, filterForm }">
                  <el-col :span="8">
                     <el-form-item label="部门名称">
                        <el-input size="small" @change="handleQuery" v-model="filterForm.deptName" clearable
                           style="width: 100%" @keyup.enter="handleQuery" />
                     </el-form-item>
                  </el-col>
                  <el-col :span="8">
                     <el-form-item label="用户名称">
                        <el-input size="small" @change="handleQuery" v-model="filterForm.userName" clearable
                           style="width: 100%" @keyup.enter="handleQuery" />
                     </el-form-item>
                  </el-col>
               </template>
               <template #tableSlot>
                  <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName" />
                  <el-table-column label="用户名称" align="center" key="userName" prop="userName" />
                  <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" />
               </template>
            </TableSelect>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitHiddenForm">保存并提交</el-button>
            </div>
         </template>
      </el-dialog>

      <!-- 任务完成配置对话框 -->
      <el-dialog :title="title" v-model="taskOpen" width="55%" append-to-body>
         <el-form ref="taskComplete" :model="formT" :rules="rules" label-width="150px">
            <el-form-item style="position: absolute; top: 40px; right: 50px;" v-if="showTask">
               <el-button type="primary" @click="submitCompletForm">提交并保存</el-button>
            </el-form-item>
            <el-card class="card-box">
               <el-row>
                  <el-col :span="24">
                     <el-form-item label="任务名称" prop="governCheckTaskName">
                        <el-input v-model="formT.governCheckTaskName" placeholder="请输入任务名称" :disabled="true" />
                     </el-form-item>
                  </el-col>
               </el-row>
               <el-row>
                  <el-col :span="24">
                     <el-form-item label="任务类型" prop="governTaskTypeName">
                        <el-input v-model="formT.governTaskTypeName" placeholder="请输入任务类型" :disabled="true" />
                     </el-form-item>
                  </el-col>
               </el-row>
               <el-row>
                  <el-col :span="12">
                     <el-form-item label="检查类型" prop="governCheckTaskTypeName">
                        <el-select v-model="formT.governCheckTaskTypeName" placeholder="请选择检查类" :disabled="true"
                           style="width: 100%">
                           <el-option v-for="value in addTaskTypeList" :key="value" :label="value" :value="value" />
                        </el-select>
                     </el-form-item>
                  </el-col>
                  <el-col :span="12">
                     <el-form-item label="任务周期">
                        <el-date-picker v-model="formT.dateRange" value-format="YYYY-MM-DD" type="daterange"
                           :disabled="true" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                           style="width: 100%" />
                     </el-form-item>
                  </el-col>
               </el-row>
               <el-row>
                  <el-col :span="12">
                     <el-form-item label="企业状态">
                        <el-radio-group v-model="formT.governCompanyStatus" :disabled="!showTask">
                           <el-radio :label="0">正常</el-radio>
                           <el-radio :label="1">停业整顿</el-radio>
                        </el-radio-group>
                     </el-form-item>
                  </el-col>
                  <el-col :span="12">
                     <el-form-item label="是否涉及">
                        <el-radio-group v-model="formT.governInvolve" :disabled="!showTask" @change="handleChange">
                           <el-radio :label="0">不涉及</el-radio>
                           <el-radio :label="1">涉及</el-radio>
                        </el-radio-group>
                     </el-form-item>
                  </el-col>
               </el-row>
            </el-card>
            <el-card class="card-box" v-if="hidedenFlag">
               <template #header><span>危险源</span></template>
               <el-table v-loading="loading" :data="taskPlanList" @selection-change="handleSelectionChange">
                  <el-table-column type="selection" width="40" align="center" />
                  <el-table-column label="序号" width="55" type="index" align="center" prop="governTaskPlanId" />
                  <el-table-column label="危险源名称" align="center" prop="governInvolveDanger"
                     :show-overflow-tooltip="true" />
                  <el-table-column label="检查时间" align="center" prop="governTaskCompleteTime" width="160">
                     <template #default="scope">
                        <span>{{ parseTime(scope.row.governTaskCompleteTime) }}</span>
                     </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" width="400" class-name="small-padding fixed-width">
                     <template #default="scope">
                        <el-button v-if="scope.row.governCheckVerify == null || scope.row.governCheckVerify == 0"
                           type="text" icon="Edit" @click="handleDangerTaskStatus(scope.row)" :disabled="!showTask"
                           v-hasPermi="['check:governCheckTaskInfo:edit']">确认已完成检查</el-button>
                        <el-button v-if="scope.row.governCheckVerify != null && scope.row.governCheckVerify == 1"
                           type="text" icon="Edit" @click="handleDangerTaskStatus(scope.row)" :disabled="true"
                           v-hasPermi="['check:governCheckTaskInfo:remove']">已确认</el-button>
                     </template>
                  </el-table-column>
               </el-table>
            </el-card>
         </el-form>
      </el-dialog>


   </div>
</template>

<script setup name="GovernCheckTaskInfo">
// 文件上传组件
import FileUpload from "@/components/FileUpload";
import {
   getCheckType, getDeviceLevel, getCheckLevel, getHiddenType, getHiddenLevel, getHiddenSource, getWeiHaiType, getZhiLiType,
   listGovernCheckTaskInfo, governCheckTask, taskSalvage, governCheckTaskItem, governCheckTaskItemId, getGovernTaskInfo,
   delGovernCheckTaskInfo, addGovernCheckTaskInfo, modifyGovernCheckTaskInfo, governHiddenInventory
} from "@/api/check/governCheckTaskInfo";
import { listGovernCheckTaskPlan, updateGovernCheckTaskPlan } from "@/api/check/governCheckTaskPlan";
import { deptTreeSelect, listUser } from "@/api/system/user";
import TableSelect from "@/components/tableSelect/index";


const { proxy } = getCurrentInstance();

// 新增模态框
const taskList = ref([]);
const addOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const checkTaskList = ref([]);
const taskTypeList = ref([]);
const addTaskTypeList = ref([]);


// 隐患录入模态框
const hiddenOpen = ref(false);
const files = ref([]);
const deptNameList = ref([]);
const deviceList = ref([]);
const sourceList = ref([]);
const checkList = ref([]);
const hiddenLevelList = ref([]);
const hiddenTypeList = ref([]);
const WeiHaiTypeList = ref([]);
const ZhiLiTypeList = ref([]);
const userTableSelect = ref();
const governCheckNameList = ref([]);
const deductPointList = ref([]);
const changeDept = (val, label) => {
   formH.value.deptName = label;
   formH.value.deptId = val;
};
const userType = ref("");
const onInnerOpen = (type) => {
   userType.value = type;
   const {
      rectificationUserId,
      rectificationUserName,
      reviewUserId,
      reviewUserName,
   } = form.value;
   let values = (rectificationUserId && rectificationUserId.split(",")) || [];
   let names = (rectificationUserName && rectificationUserName.split(",")) || [];
   if (type == "reviewUser") {
      values = (reviewUserId && reviewUserId.split(",")) || [];
      names = (reviewUserName && reviewUserName.split(",")) || [];
   }
   userTableSelect.value.open(
      values.map((item, index) => ({
         value: values[index],
         label: names[index],
      }))
   );
};
const onUserConfirm = (data) => {
   const values = data.map((item) => item.value);
   const names = data.map((item) => item.label);
   if (userType.value == "reviewUser") {
      formH.value.reviewUserId = values.join(",");
      formH.value.reviewUserName = names.join(",");
   } else {
      formH.value.rectificationUserId = values.join(",");
      formH.value.rectificationUserName = names.join(",");
   }
};

// 任务完成
const taskOpen = ref(false);
const taskPlanList = ref([]);
const hidedenFlag = ref(false);

//查看任务
const showTask = ref(true);

const data = reactive({
   form: {},
   formH: {
      deptNameList: [],
      deviceList: [],
      sourceList: [],
      checkList: [],
      hiddenLevelList: [],
      hiddenTypeList: []
   },
   formT: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      governCheckTaskName: undefined,
      governTaskTypeName: undefined,
      governCheckTaskTypeName: undefined
   },
   rules: {
      hiddenInventoryName: [{ required: true, message: "隐患名称不能为空", trigger: "blur" }],
      hiddenSource: [{ required: true, message: "隐患来源不能为空", trigger: "blur" }],
      hiddenType: [{ required: true, message: "隐患分类不能为空", trigger: "blur" }],
      hiddenLevel: [{ required: true, message: "隐患登记不能为空", trigger: "blur" }],
      hiddenCheckType: [{ required: true, message: "排查类型不能为空", trigger: "blur" }],
      harmType: [{ required: true, message: "危害类型不能为空", trigger: "blur" }],
      dangerManageType: [{ required: true, message: "隐患治理类型不能为空", trigger: "blur" }],
      rectificationEndTime: [{ required: true, message: "隐患治理期限不能为空", trigger: "blur" }],
      hiddenInfo: [{ required: true, message: "隐患描述不能为空", trigger: "blur" }],
      rectificationUserName: [{ required: true, message: "整改人不能为空", trigger: "blur" }],
      reviewUserName: [{ required: true, message: "复核人不能为空", trigger: "blur" }],

      governTaskTypeName: [{ required: true, message: "任务类型不能为空", trigger: "blur" }],
      governCheckTaskTypeName: [{ required: true, message: "检查类型不能为空", trigger: "blur" }],
   }
});

const { queryParams, form, formH, formT, rules } = toRefs(data);

const fChangeDevice = (e) => {
  formH.value.deviceId = e.id;
  formH.value.deviceName = e.deviceName;
}

function handleChange(value) {
   if (value == 1) {
      hidedenFlag.value = true;
   } else {
      hidedenFlag.value = false;
   }
}
/** 查询检验类型列表 */
function getDicList() {
   getCheckType().then(response => {
      taskTypeList.value = response.data;
   });
   deptTreeSelect().then((response) => {
      deptNameList.value = response.data;
   });
   getDeviceLevel().then(response => {
      deviceList.value = response.data;
   });
   getHiddenSource().then(response => {
      sourceList.value = response.data;
   });
   getCheckLevel().then(response => {
      checkList.value = response.data;
   });
   getHiddenLevel().then(response => {
      hiddenLevelList.value = response.data;
   });
   getHiddenType().then(response => {
      hiddenTypeList.value = response.data;
   });
   getWeiHaiType().then(response => {
      WeiHaiTypeList.value = response.data;
   });
   getZhiLiType().then(response => {
      ZhiLiTypeList.value = response.data;
   });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.governTaskInfoId);
   single.value = selection.length != 1;
   multiple.value = !selection.length;
}

/** 查询参数列表 */
function getList() {
   loading.value = true;
   listGovernCheckTaskInfo(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
      taskList.value = response.rows;
      total.value = response.total;
      loading.value = false;
   });
}


/** 新增任务表单重置 */
function resetAdd() {
   form.value = {
      taskId: undefined,
      checkTaskTypeName: undefined
   };
   proxy.resetForm("addTask");
}

/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
}

/** 新增按钮操作 */
function handleAddTask() {
   resetAdd();
   addOpen.value = true;
   title.value = "新增任务";
   getAddcheckTaskList();
}

/** 新增选择任务列表 */
function getAddcheckTaskList() {
   loading.value = true;
   governCheckTask().then(response => {
      checkTaskList.value = response.rows;
      addTaskTypeList.value = taskTypeList.value.filter(value => value !== '企业自查');
      loading.value = false;
   });
}

/** 新增任务提交 */
function submitForm() {
   proxy.$refs["addTask"].validate(valid => {
      if (valid) {
         addGovernCheckTaskInfo(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            addOpen.value = false;
            getList();
         });
      }
   });
}
/** 取消按钮 */
function cancel() {
   addOpen.value = false;
   resetAdd();
}


/** 删除按钮操作 */
function handleDelete(row) {
   const taskIds = row.taskId || ids.value;
   proxy.$modal.confirm('是否确认删除参数编号为"' + taskIds + '"的数据项？').then(function () {
      console.log(taskIds);
      return delGovernCheckTaskInfo(taskIds);
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}

/** 任务分解 */
function handleTaskDecompose(row) {
   loading.value = true;
   const governTaskInfoId = row.governTaskInfoId;
   taskSalvage({ governTaskInfoId: governTaskInfoId }).then(response => {
      proxy.$modal.msgSuccess("操作成功");
      loading.value = false;
   });
}

/** 隐患录入按钮操作 */
function handleAddHidden(row) {
   resetHidden();
   hiddenOpen.value = true;
   title.value = "隐患录入";
   files.value = [];
   const governCheckTaskId = row.governCheckTaskId;
   governCheckTaskItem({ governCheckTaskId: governCheckTaskId }).then(response => {
      governCheckNameList.value = response.rows;
   });

   formH.value.governTaskInfoId = row.governTaskInfoId;
   formH.value.governCheckTaskId = governCheckTaskId;
   formH.value.hiddenCheckType = row.governCheckTaskTypeName;
   formH.value.checkLevel = '公司级';

}
function governCheckChanged(row) {
   deductPointList.value = [];
   const foundItem = governCheckNameList.value.find(item => item.governCheckTaskItemId === row);
   if (!foundItem) {
      return;
   }
   formH.value.governCheckBasis = foundItem.governCheckBasis;
   formH.value.governCheckWay = foundItem.governCheckWay;
   formH.value.governApplicablePlace = foundItem.governApplicablePlace;
   governCheckTaskItemId(row).then(response => {
      if (response.data) {
         // deductPointList.value.push(response.data);
         deductPointList.value = response.data;
         // 设置默认值
         formH.value.governCheckTaskItemScoreId = response?.data[0]?.governCheckTaskItemScoreId;
         formH.value.governDeductIllustrate = response?.data[0]?.governDeductIllustrate;
         formH.value.governDeductPoint = response?.data[0]?.governDeductPoint;
      } else {
         formH.value.governCheckTaskItemScoreId = undefined;
         formH.value.governDeductIllustrate = undefined;
         formH.value.governDeductPoint = undefined;
      }
   });
}
function governCheckScoreChanged(row) {
   formH.value.governCheckTaskItemScoreId = row.governCheckTaskItemScoreId;
   formH.value.governDeductIllustrate = row.governDeductIllustrate;
   formH.value.governDeductPoint = row.governDeductPoint;
}

/** 隐患录入表单重置 */
function resetHidden() {
   formH.value = {};
   proxy.resetForm("addHidden");
}

/** 隐患录入表单重置 */
function resetSubHidden() {
   formH.value = {};
}

/** 隐患录入提交按钮 */
function submitHiddenForm() {
   proxy.$refs["addHidden"].validate(valid => {
      if (valid) {
         console.log(formH.value);
         governHiddenInventory(formH.value).then(response => {
            proxy.$modal.msgSuccess("提交成功！");
            hiddenOpen.value = false;
            getList();
         });
      }
   });
}

/** 任务查看 **/
function showTaskDetail(row) {
   const governTaskInfoId = row.governTaskInfoId;
   resetTask(governTaskInfoId);
   getTaskPlanList(governTaskInfoId);
   taskOpen.value = true;
   showTask.value = false;
   title.value = "任务查看";
   handleChange(row.governInvolve);
}


/** 任务完成按钮操作 */
function handleTask(row) {
   const governTaskInfoId = row.governTaskInfoId;
   resetTask(governTaskInfoId);
   getTaskPlanList(governTaskInfoId);
   taskOpen.value = true;
   showTask.value = true;
   title.value = "任务完成";
   handleChange(row.governInvolve);
}

/** 任务完成表单重置 */
function resetTask(governTaskInfoId) {
   loading.value = true;
   getGovernTaskInfo(governTaskInfoId).then(response => {
      formT.value = response.data;
      if (formT.value.governInvolve == 1) {
	     hidedenFlag.value = true;
	  }
      formT.value.dateRange = [response.data.governTaskStartTime, response.data.governTaskEndTime];
      loading.value = false;
   });
   proxy.resetForm("taskComplete");
}

/** 查询参数列表 */
function getTaskPlanList(governTaskInfoId) {
   loading.value = true;
   queryParams.value.governTaskInfoId = governTaskInfoId;
   // proxy.addDateRange(queryParams.value, dateRange.value)
   listGovernCheckTaskPlan({ 'governTaskInfoId': governTaskInfoId }).then(response => {
      taskPlanList.value = response.rows;
      total.value = response.total;
      loading.value = false;
   });
}

/** 修改任务完成结果 */
function submitCompletForm() {
   loading.value = true;
   proxy.$refs["taskComplete"].validate(valid => {
      if (valid) {
         // console.log(formT.value);
         const params = {
            governTaskInfoId: formT.value.governTaskInfoId,
            governCompanyStatus: formT.value.governCompanyStatus,
            governInvolve: formT.value.governInvolve
         };
         modifyGovernCheckTaskInfo(params).then(response => {
            proxy.$modal.msgSuccess("操作成功");
            loading.value = false;
         });
      }
   });
}


/** 修改危险源任务状态 */
function handleDangerTaskStatus(row) {
   const governTaskPlanId = row.governTaskPlanId;
   updateGovernCheckTaskPlan({ "governTaskPlanId": governTaskPlanId, "governCheckVerify": 1 }).then(response => {
      proxy.$modal.msgSuccess(response.msg);
      getTaskPlanList(row.governTaskInfoId);
   });
}

watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      formH.value.hiddenFileId = "";
	  formH.value.hiddenFileUrl = "";
	  formH.value.hiddenFileName = "";
    } else {
      formH.value.hiddenFileName = newData.value[0].name;
      formH.value.hiddenFileId = newData.value[0].id;
      formH.value.hiddenFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);


getList();
getDicList();
</script>
